//
//  geeAppDelegate.m
//  gee
//
//  Created by apple on 11-5-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "geeAppDelegate.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h"



@implementation geeAppDelegate


@synthesize window=_window;

@synthesize tabBarController=_tabBarController;





- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window.rootViewController = self.tabBarController;
    self.tabBarController.selectedIndex = 3;
    NSURL *url = [NSURL URLWithString:@"http://api.loc/cookie.php"];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
   // [request setPostValue:@"gakaki" forKey:@"username"];
    //[request setPostValue:@"z5896321"  forKey:@"password"];
    
 //   [request addPostValue:@"gakaki" forKey:@"username"];
   // [request addPostValue:@"z5896321" forKey:@"password"];
    

    //Create a cookie
    NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
    [properties setValue:[[[UIDevice currentDevice] uniqueIdentifier] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:NSHTTPCookieValue];
    [properties setValue:@"PHPSESSID" forKey:NSHTTPCookieName];
    [properties setValue:@".api.loc" forKey:NSHTTPCookieDomain];
  //  [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
    
    
    [properties setValue:[NSDate dateWithTimeIntervalSinceNow:60] forKey:NSHTTPCookieExpires];
    
    
    
    [properties setValue:@"/" forKey:NSHTTPCookiePath];
    NSHTTPCookie *cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
    

    [request setUseCookiePersistence:NO];
    [request setRequestCookies:[NSMutableArray arrayWithObject:cookie]];

    
    //[ASIHTTPRequest setSessionCookies:nil];
   
    [request startAsynchronous];
    
   
    
    [self.window makeKeyAndVisible];
    return YES;
}


#pragma mark -
#pragma mark HTTP Response Handlers

- (void)requestFinished:(ASIHTTPRequest *)request {
    // Use when fetching text data
    CLog(@"HTTP Fetch: %@", [request responseString]);
    
    
    CLog(@"Object jsonkit is : %@", [[request responseString] objectFromJSONString]);

    
    //保存用户名密码
    [[NSUserDefaults standardUserDefaults] setValue:@"gakaki" forKey:@"username"];
    [[NSUserDefaults standardUserDefaults] setValue:@"z5896321" forKey:@"first password"];
    
    id object =  [[request responseString] objectFromJSONString];
    //NSLog(@"%@", NSStringFromClass([object class]));
    
    NSDictionary *deserializedData =  [[request responseString] objectFromJSONString];
    NSLog(@"%@", deserializedData);
    
    
    
    NSLog(@"%@", [deserializedData valueForKey:@"status"]);
    NSLog(@"%@", [[deserializedData valueForKey:@"data"] valueForKey:@"password"]);
    
    [[NSUserDefaults standardUserDefaults] setValue:@"z5896321" forKey:@"site_session_id"];
    
    
    
    
    
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    NSLog(@"Error: %@", [error description]);

}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}
*/

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
}
*/

@end
